<?php
/**
 * PHP heeft tegenwoordig ook mooie built-in classes
 * http://php.net/manual/en/class.iterator.php
 * 
 * Dit vond ik een mooie implemetatie
 */
class FibonacciSequence implements Iterator
{
    protected $limit = 0;
    protected $key = 0;
    
    public function __construct ($limit = 0)
    {
        $this->limit = (integer) $limit;
    }
    
    public function current ()
    {
        return round(
        (pow(((1 + sqrt(5)) / 2), $this->key) - pow(
        (- 1 / (1 + sqrt(5)) / 2), $this->key)) /
         sqrt(5), null);
    }
    
    public function key ()
    {
        return $this->key;
    }
    
    public function next ()
    {
        $this->key ++;
    }
    
    public function rewind ()
    {
        $this->key = 0;
    }
    
    public function valid ()
    {
        return $this->key < $this->limit;
    }
}

foreach (new FibonacciSequence(30) as $number) {
    printf("%d\n", $number);
}

